import {observable} from 'mobx';
import {Tools, Modal} from 'weaver-mobile';
import { ShareAuth, /*Reply*/ } from 'weaver-mobile-page';
import * as API from '../../api/share';
import isFunction from 'lodash/isFunction';
// import RichText from '../../util/richText';
const getLabel = Tools.getLabel;
const alert = Modal.alert;

export default class ShareStore {
    
    parent = null;
    path = '';//前缀路径
    saveBackFunc = null;//保存
    @observable id = '';// 文档id，现在只在共享页面使用
    constructor({parent, path, saveBackFunc,id}) {
      this.id = id;
      this.parent = parent;
      this.path = path;
      this.shareTypeStore = new ShareAuth.Store({route: path, id: 'type'});
      this.saveBackFunc = saveBackFunc;
    }
    
    @observable shareCanAdd = false;
    @observable shareCanDelete = false;
    @observable shareCondition = null;
    @observable shareTypeStore = null;
    @observable defaultShareList = [];//默认共享列表
    @observable nonDefaultShareList = [];//非默认共享列表  
     /**
   * 初始化共享页面
   */
  initShare = () => {
    if (!this.id) {
      return;
    }
    this.getShareList();
    API.shareCanEdit({id: this.id}).then((result) => {
      this.shareCanAdd = result.hasAddBtn === '1';
      this.shareCanDelete = result.hasDelBtn === '1';
    });
    API.getShareCondition({docid: this.id}).then((result) => {
      this.shareCondition = result.conditions;
    });
  }

  getShareList = () => {
    API.shareList({id: this.id}).then((result) => {
      this.defaultShareList = result.defaultShareList;
      this.nonDefaultShareList = result.nonDefaultShareList;
    });
  }
    /**
   * 新增共享
   * @param params
   */
  addShare = (params) => {
    API.saveShare({...params, id: this.id}).then(() => {
      this.getShareList();
    })
  }
  /**
   * 共享提交
   * @param history
   */
  afterSave = (history) => {
    API.afterSave({id: this.id}).then((result) => {
      if (isFunction(this.saveBackFunc)) {
        this.saveBackFunc({id: this.id, history});
      }
    });
  }
    /**
   * 删除共享
   * @param shareId
   */
  delShare = (shareId) => {
    alert(getLabel(131329,'信息确认'), getLabel(388758,'确认要删除吗？'),
      [
        { text: getLabel(31129, '取消')},
        {
          text: getLabel(826, '确定'),
          onPress: () => {
            API.deleteShare({docid: this.id, shareId: shareId}).then((result) => {
              this.getShareList();
            })
          }
        },
      ]);
  }

}